﻿@{
    Layout = null;
    var tableName = PageViewModel.PageTableID(this);
    var tableToolBar = PageViewModel.PageTableToolbar(this);
    var tableToolBarSearch = PageViewModel.PageTableToolbarSearch(this);
    var tableToolBarReset = PageViewModel.PageTableToolbarReset(this);

    var editUserDlgName = PageViewModel.PageEditDialog(this);
    var editUserFunctionName = PageViewModel.PageFunctionName(this, "edit_user");
    var saveUserFunctionName = PageViewModel.PageFunctionName(this, "save_user");
    var deleteFunName = PageViewModel.PageFunctionName(this, "delete_user");

    var userPasswordFmt = PageViewModel.PageFunctionName(this, "edit_user_pwd_fmt");
    var userPasswordResetFun = PageViewModel.PageFunctionName(this, "edit_user_pwd_reset");
    var userPasswordResetFunDlg = PageViewModel.ControlID(this, "edit_user_pwd_reset_dlg");

}

<div style="height:100%">
    <script type="text/javascript">
        function @editUserFunctionName (id) {
            var dlg = $dlg('@editUserDlgName');
            dlg.dialog({
                title: '用户信息', width: 950, height: 500,
                closed: false, cache: false, modal: true, maximizable: false, resizable: true,
                href: '~/Systems/_EditUserNoDep/' + id,
                onResize: function () {
                    $(this).dialog('center');
                },
                buttons: [
                    {
                        text: "保 存",
                        handler: function () { @saveUserFunctionName (); },
                        iconCls: "icon-save"
                    }, {
                        text: "取 消",
                        handler: function () {
                            $("#@editUserDlgName").dialog("close");
                        },
                        iconCls: "icon-cancel"
                    }
                ],
                onLoad: function () {
                    LoadObject(id, "~/Systems/GetEditUserByID/", function (item) {
                        if (item != null) {
                            var form = $("#@editUserDlgName").find("form");
                            form.form("load", item.User);
                            if (item.User.Enable) {
                                form.find("#Enable").switchbutton("check");
                            } else {
                                form.find("#Enable").switchbutton("uncheck");
                            }

                            form.find("#PasswordHash").passwordbox("disable");
                        }
                    });
                }
            });
        }

        function @saveUserFunctionName () {
            var form = $("#@editUserDlgName").find("form");
            if (form.form('enableValidation').form('validate')) {
                var user = form.form_to_json();
                user.Enable = form.find("#Enable").switchbutton("options").checked;

                $.ajax({
                    url: "~/Systems/SaveUser",
                    type: "POST",
                    data: user,
                    success: function (e) {
                        if (e.Result) {
                            $.messager.alert('保存成功', '保存成功!', 'info', function () {
                                $('#@editUserDlgName').dialog('close');
                                $('#@tableName').datagrid('reload');
                            });
                        } else {
                            $.messager.alert('保存失败', '保存失败: ' + e.Msg, 'error');
                        }
                    },
                    error: function (e) {
                        ShowAjaxError(e);
                    }
                });
            }
        }
    </script>

    <script type="text/javascript">
        function @deleteFunName (id){
            if (id > 0 && confirm("确认删除用户信息?")) {
                $.ajax({
                    async: false,
                    method: "DELETE",
                    url: $.Content("~/Systems/DeleteUser/" + id),
                    context: $("#@tableName"),
                    success: function (e) {
                        if (e.Result) {
                            this.datagrid('reload');
                        } else {
                            $.messager.alert('删除数据', '删除失败: ' + e.Msg, 'error');
                        }
                    },
                    error: function (e1, e2, e3) {
                        ShowAjaxError(e1, "删除数据");
                        this.datagrid('reload');
                    }
                });
            }
        }
    </script>

    <script type="text/javascript">
    function @tableToolBarSearch {
        $('#@tableName').datagrid('load', {
            FullName: $("#@tableToolBar").find("#FullName").textbox("getValue"),
        });
    }

    function @tableToolBarReset {
        $("#@tableToolBar").find("#FullName").textbox("setValue", "");
        @tableToolBarSearch;
    }
    </script>

    <script type="text/javascript">
        function @userPasswordFmt (v, r, i) {
            if (!r.IsInternal) {
                var op = "<div class='grid_operation_grey' onclick='@userPasswordResetFun (" + r.Id + ")'>重置</div>";
                return op;
            } return '';
        }

        function @userPasswordResetFun (id) {
            var dlg = $dlg('@userPasswordResetFunDlg');
            dlg.dialog({
                title: '重置密码', width: 500, height: 220,
                closed: false, cache: false, modal: true, maximizable: false, resizable: true,
                href: '~/Systems/_ResetPassword?ID=' + id,
                onResize: function () {
                    $(this).dialog('center');
                },
                buttons: [
                    {
                        text: "确 定",
                        handler: function () {
                            var form = $dlg('@userPasswordResetFunDlg').find("form");
                            var pwd = form.find("#_User_Rest_Password").textbox("getValue");
                            var item = {
                                ID: parseInt(form.find("#_User_Rest_Id").val()),
                                Password: pwd,
                                RePassword: pwd
                            };
                            $.ajax({
                                async: false,
                                method: 'post',
                                contentType: "application/json",
                                url: "~/Systems/ResetUserPassword",
                                data: JSON.stringify(item),
                                success: function (e) {
                                    if (e) {
                                        if (e.Result) {
                                            $('#@userPasswordResetFunDlg').dialog('close');
                                            $.messager.alert('成功', e.Msg, 'info');
                                        } else {
                                            $.messager.alert('失败', e.Msg, 'error');
                                        }
                                    } else {
                                        $.messager.alert('失败', '系统遇到错误', 'error');
                                    }
                                },
                                error: function (e) {
                                    ShowAjaxError(e);
                                }
                            });
                        },
                        iconCls: "icon-save"
                    }, {
                        text: "取 消",
                        handler: function () {
                            $("#@userPasswordResetFunDlg").dialog("close");
                        },
                        iconCls: "icon-cancel"
                    }
                ]
            });
        }
    </script>

    <!-- -->
    <table id="@tableName" class="easyui-datagrid" style="width:100%;height:100%;" rownumbers="true" fit="true" fitColumns="false"
           pagination="true" pageSize="@SystemSettingService.Instance.PageSize" pageList="@SystemSettingService.Instance.PageList"
           data-options="striped:true,border:false,singleSelect:true,url:'~/Systems/GetUsers',method:'get',toolbar:'#@tableToolBar'">
        <thead>
            <tr>
                <th data-options="field:'_edit',align:'center',formatter:function(v,r,i){
                        if(!r.IsInternal) {
                            return GridRowEditFmt('@editUserFunctionName', r.Id);
                        }
                    }" width="50">修改</th>
                <th data-options="field:'_remove',align:'center',formatter:function(v,r,i){
                        if(!r.IsInternal) {
                            return GridRowDeleteFmt('@deleteFunName', r.Id);
                        }
                    }" width="50">删除</th>
                <th data-options="field:'Code',width:120">人员编号</th>
                <th data-options="field:'FullName',width:120">人员姓名</th>
                <th data-options="field:'Gender',width:60,align:'center'">性别</th>
                <th data-options="field:'Status',width:80,align:'center',formatter:function(v,r,i){
                        var v = GetUrlObject('~/Systems/GetAppDictionaryByTID?typeCode=USER_STATUS&id='+v);
                        if (v != null) { return v.Name; }
                    }">账户状态</th>
                <th data-options="field:'UserName',width:140">系统登录名</th>
                <th data-options="field:'Enable',width:80,align:'center',formatter:$.CheckdFormat">可登录</th>
                <th data-options="field:'_epwd',width:80,align:'center',formatter:@userPasswordFmt">密码</th>
                <th data-options="field:'PhoneNumber',align:'center',width:100">电话</th>
                <th data-options="field:'Email',width:160">Email</th>
                <th data-options="field:'Address',width:160">联系地址</th>
                <th data-options="field:'AccessFailedCount',width:80,align:'center',formatter:function(v,r,i){ return v+'次';}">登录错误</th>
                <th data-options="field:'Remark',width:160">备注</th>
                <th data-options="field:'ChangedTime',width:160,align:'center',formatter:$.DateTimeFmt">修改时间</th>
                <th data-options="field:'CreatedTime',width:160,align:'center',formatter:$.DateTimeFmt">创建时间</th>
            </tr>
        </thead>
    </table>
    <div id="@tableToolBar" class="easyui-panel table-toolbar">
        <a href="javascript:@editUserFunctionName ()" class="easyui-linkbutton t-btn"><i class="fa fa-plus-circle" style="color:green"></i><span>新增</span></a>
        <ul class="query-body">
            <li>
                <span>人员姓名：</span>
                <input id="FullName" class="easyui-textbox" data-options="prompt:'请输入人员姓名'" />
            </li>
            <li>
                <a href="javascript:@tableToolBarSearch" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a>
                <a href="javascript:@tableToolBarReset" class="easyui-linkbutton" data-options="iconCls:'icon-reload'">重置</a>
            </li>
        </ul>
    </div>

</div>